package com.wms.service;

import com.mes.entity.User;
import com.yy.base.common.dao.Dao;
import com.yy.base.common.service.BaseService;
import com.yy.base.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class SalesoutletService extends BaseService {
	@Autowired
	private Dao dao;
	private String uuid = null;

	//查询工单状态及工单信息
	public List<Map<String, Object>> getjobStatusList(String rfid) {
		String sql = "SELECT    " +
				"          zy.CODE AS ZYCODE,    " +
				"          kc.wms_storage_batch_id as BATCHID, " +
				"        zy.emeso_job_order_id as JOBID,  " +
				"          WL.    " +
				"        VALUE    " +
				"          || ' - ' || WL.NAME PRODUCT,    " +
				"          jh.color YSNAME,  " +
				"          cm.CODE AS CMCODE,    " +
				"          sc.c_uom_id AS DWID,    " +
				"          to_char ( xs.Sales_Date, 'yyyy-mm-dd hh24:mi:ss' ) TIMES,    " +
				"          xs.emes_customer_id AS YHNAME,    " +
				"          xs.address AS DIZHI     " +
				"        FROM    " +
				"          emeso_job_order zy,    " +
				"          m_product wl,    " +
				"          emeso_manuorder sc,    " +
				"          wms_sales_order_detail mx,    " +
				"          wms_sales_order xs,    " +
				"          wms_storage_batch kc,  " +
				"          emesp_planorder jh   " +
				"          LEFT JOIN emes_size cm ON jh.emes_size_id = cm.emes_size_id     " +
				"        WHERE    " +
				"          zy.emeso_manuorder_id = sc.emeso_manuorder_id     " +
				"          and zy.code = kc.box_package_code  " +
				"          AND sc.emesp_planorder_id = jh.emesp_planorder_id     " +
				"          AND jh.wms_sales_order_detail_id = mx.wms_sales_order_detail_id     " +
				"          AND mx.wms_sales_order_id = xs.wms_sales_order_id     " +
				"          AND zy.m_product_id = wl.m_product_id  and zy.status != 'OK'   " +
				"          AND (zy.CODE = '"+rfid+"' OR ZY.logistics_code = '"+rfid+"')";
		return dao.query(sql);
	}





	/**
	 * 往inout表中插入成品信息
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public boolean inStorageInOut( int qty, String uomID,String jobid, User user,String jobcode,String BATCHID) {
		boolean result = false;
		String uuids = StringUtil.getUUID();

		String sql = "insert into wms_storage_in_out "
				+ " (Wms_Storage_In_Out_ID,Wms_Storage_Batch_ID,Qty,C_Uom_ID,Opt_Mode,Opt_Code,Data_Id,Status,"
				+ "AD_CLIENT_ID, AD_ORG_ID,ISACTIVE, CREATED, CREATEDBY, UPDATED, UPDATEDBY)" +
				" values(?,?,?,?,?,?,?,'OK',?,?,?,now(),?,now(),?)";


		result =  dao.executeSql(sql, uuids,BATCHID,qty,uomID, "销售出库",jobcode,jobid, user.getClientId(), user.getOrgId(), "Y", user.getId(), user.getId()) > 0;

		return result;
	}

	/**
	 * 修改Emeso_job_order表状态(代表该作业工单已完工入库)
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public boolean uodateStatus(String id) {
		boolean result = false;


		String updatesql = "UPDATE Emeso_job_order SET status = 'OK' WHERE Emeso_job_order_id = ?";

		result = dao.executeSql(updatesql,  id ) > 0;


		return result;
	}



}